// pages/index/wineShop/orderPay/chooseCoupon/chooseCoupon.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    useable: [
      // { 'price': 20, 'KTV': 'XXXktv', 'day': 2, 'checked': '../../../../../imgs/checked.png' },
      // { 'price': 20, 'KTV': 'XXXktv', 'day': 2, 'checked': '../../../../../imgs/noCheck.png' },
      // { 'price': 20, 'KTV': 'XXXktv', 'day': 2, 'checked': '../../../../../imgs/noCheck.png' },
      // { 'price': 20, 'KTV': 'XXXktv', 'day': 2, 'checked': '../../../../../imgs/noCheck.png' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    console.log('options1111111',options)
    if(options.sty==2){
      app.wxrequest({
        url: app.globalData.baseUrl + '/api/serve/coupon_list',
        data: {
          type: options.sty,
          session_key: app.gs()
        },
        success: function (res) {
          wx.hideLoading()
          console.log('ressss', res)
          let useable = res.data.data;
          if (useable) {
            for (let i = 0; i < useable.length; i++) {
              useable[i].checked = '../../../../../imgs/noCheck.png'
            }
            useable[0].checked = '../../../../../imgs/checked.png'
          } else {
            return
          }
          _this.setData({
            useable: useable,
            coupon_id: useable[0].id,
            amount: useable[0].amount,
            type: useable[0].type,
            com_id: options.com_id,
            com_price:options.com_price,
            mobile:options.mobile,
            num:options.num,
            coupon_name: useable[0].cou_name,
            price: options.price,
            // shopname:options.shopname
          })
        }
      })

    }else if(options.sty==3){
      app.wxrequest({
        url: app.globalData.baseUrl + '/api/serve/coupon_list',
        data: {
          type: options.sty,
          session_key: app.gs()
        },
        success: function (res) {
          wx.hideLoading()
          console.log('ressss', res)
          let useable = res.data.data;
          if (useable) {
            for (let i = 0; i < useable.length; i++) {
              useable[i].checked = '../../../../../imgs/noCheck.png'
            }
            useable[0].checked = '../../../../../imgs/checked.png'
          } else {
            return
          }
          _this.setData({
            useable: useable,
            coupon_id: useable[0].id,
            amount: useable[0].amount,
            type: useable[0].type,
            order_id: options.order_id,
            house_num: options.houNum
          })
          // console.log(options.order_id)
        }
      })
    }else if (options.sty==1){
      app.wxrequest({
        url: app.globalData.baseUrl + '/api/serve/coupon_list',
        data: {
          type: options.sty,
          session_key: app.gs()
        },
        success: function (res) {
          wx.hideLoading()
          console.log('ressss', res)
          let useable = res.data.data;
          if (useable) {
            for (let i = 0; i < useable.length; i++) {
              useable[i].checked = '../../../../../imgs/noCheck.png'
            }
            useable[0].checked = '../../../../../imgs/checked.png';
            let coupon = 1;
            _this.setData({
              coupon:coupon
            })
          } else {
            return
          }
          console.log('options------',options)
          _this.setData({
            useable: useable,
            coupon_id: useable[0].id,
            amount: useable[0].amount,
            type: useable[0].type,
            coupon_name: useable[0].cou_name,
            date:options.date,
            count:options.count,
            mobile:options.mobile,
            appoint_date: options.appoint_date,
            shopname:options.shopname
          })
        }
      })
    }
    
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseCoupo(e) {
    var that = this;
    console.log(e)
    var useable = this.data.useable;
    var index = e.currentTarget.dataset.index;
    for (var i in useable) {
      useable[i].checked = '../../../../../imgs/noCheck.png'
    }
    useable[index].checked = '../../../../../imgs/checked.png'
    this.setData({
      useable: useable,
      coupon_id: useable[index].id,
      amount: useable[index].amount,
      type: useable[index].type,
      coupon_name: useable[index].useable
    })
  },
  sureCoupon(e){
    wx.navigateBack({
      delta: 1
    })
  },
  confirmCoupon(e){
    let _this = this;
   
    if(_this.data.type == 2){
      wx.request({
        url: app.globalData.baseUrl + '/api/serve/coupon_select',
        method: 'POST',
        header: {
          'content-Type': 'application/x-www-form-urlencoded'
        },
        data: {
          session_key: app.gs(),
          order_id: _this.data.order_id,
          coupons_id: _this.data.coupon_id,
          coupons_price: _this.data.amount
        },
        success: function (res) {
          console.log(res)
        }
      })
      wx.navigateTo({
        url: '../../../groupPurchase/groupOrder/groupOrder?typ=' + _this.data.type + '&amount=' + _this.data.amount + '&coupon_id=' + _this.data.coupon_id + '&coupon=1' + '&com_price=' + _this.data.com_price + '&mobile=' + _this.data.mobile + '&order_id=' + _this.data.com_id + '&num=' + _this.data.num + '&coupon_name=' + _this.data.coupon_name + '&com_id=' + _this.data.com_id + '&price=' + _this.data.price + '&house_num=' + _this.data.house_num,
      })
    }
    if (_this.data.type == 3) {
      wx.request({
        url: app.globalData.baseUrl + '/api/serve/coupon_select',
        method: 'POST',
        header: {
          'content-Type': 'application/x-www-form-urlencoded'
        },
        data: {
          session_key: app.gs(),
          order_id: _this.data.order_id,
          coupons_id: _this.data.coupon_id,
          coupons_price: _this.data.amount
        },
        success: function (res) {
          console.log(res)
        }
      })
      
      wx.redirectTo({
        url: '../orderPay?id='+_this.data.order_id+'&coupon=1',
      })
    }

    if (_this.data.type == 1) {
      console.log('_this', _this.data)
      // wx.request({
      //   url: app.globalData.baseUrl + '/api/serve/coupon_select',
      //   method: 'POST',
      //   header: {
      //     'content-Type': 'application/x-www-form-urlencoded'
      //   },
      //   data: {
      //     session_key: app.gs(),
      //     order_id: _this.data.order_id,
      //     coupons_id: _this.data.coupon_id,
      //     coupons_price: _this.data.amount
      //   },
      //   success: function (res) {
      //     console.log(res)
      //   }
      // })
      console.log('111111111-----')
      wx.redirectTo({
        url: '../../../reservation/reserve/reserve?typ=' + _this.data.type + '&amount=' + _this.data.amount + '&coupon_id=' + _this.data.coupon_id + '&coupon=1' + '&coupon_name=' + _this.data.coupon_name + '&date=' + _this.data.date + '&count=' + _this.data.count + '&mobile=' + _this.data.mobile + '&appoint_date=' + _this.data.appoint_date + '&coupon=' + _this.data.coupon + '&amount=' + _this.data.amount + '&shopname=' + _this.data.shopname
    })
  }
  }
})